package sis.ra.yahoor.result;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.utility.Utils;


public class getYahooResults {

	public static String getYahooResultXML(String query, int resultnum)
	{
		try {
			query = URLEncoder.encode(query, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String yahooquery="http://boss.yahooapis.com/ysearch/web/v1/"+query+"?format=xml&count="+resultnum+"&filter=-porn&appid=r.n3YhLV34FvfCRDPEJEm83gFUWbioKVrN3kcjeMol6J94rCnQndPPqzuOaIVA--";
		String content=Utils.fetchContentfrURL(yahooquery);
		return content;
	}
	
	public static String totalhits(String xml)
	{
		String hit="";
		Document document = null;
		ArrayList<yahooresults> result=new ArrayList<yahooresults>();
		try {
			document = new SAXBuilder().build(new StringReader(xml));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		Namespace ns = Namespace.getNamespace("http://www.inktomi.com/");
		Element resultset=root.getChild("resultset_web", ns);
		String hits=resultset.getAttributeValue("totalhits");
		return hits;
	}
	
	public static void main(String[] args) {
		//http://boss.yahooapis.com/ysearch/web/v1/ming%20?format=xml&count=0&filter=-porn&appid=r.n3YhLV34FvfCRDPEJEm83gFUWbioKVrN3kcjeMol6J94rCnQndPPqzuOaIVA--
		/*
		 * crawl yahoo result in
		 * C:\Documents and Settings\I820753\Desktop\dataset\yahoo result_1003
		 * using COMPANY PRODUCTS pattern
		 */
		String query="Google \"AdWords Editor\"";
		int resultnum=100;		
		String productsfile="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\products.txt";
		String productscontent=Utils.readFile(productsfile);

		String [] lines=productscontent.split("\n");
		int count=0;
		for (int i=0;i<lines.length;i++)
		{
			String [] items=lines[i].split("\t");
			String company=items[0];
			if (items.length<3) continue;
			String[] products=items[2].split(" ; ");
			for (int j=0;j<products.length;j++)
			{
				if (products[j].length()<2) continue;
				String pro=products[j];//Utils.removePunctuation(products[j]);
				query=company+" "+pro;
				String content=getYahooResultXML(query,resultnum);
//				if (count>50) continue;
				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo\\"+Utils.removePunctuation(query)+".txt", "COMPANYPRODUCT: "+company+"\t"+pro, true,true);
				Utils.writeToFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo\\"+Utils.removePunctuation(query)+".txt", content,true, true);
				System.out.println(query);
				count=count+1;
			}
		}
		System.out.println(count);
		
		
//		Utils.writeFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo\\test.txt", content);
//		String content=Utils.readFile("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo\\test.txt");
//		System.out.println(content);
//		getYahooResult(content);
	}

}
